package OOAE_UWE.Assessment;

import java.util.Calendar;

public class RentalSystemDemo {
    public static void main(String[] args) {
        //create 2 rooms:
        Room room1 = new Room("301", "Deluxe", new CleanState());
        Room room2 = new Room("302", "standard", new CleanState());

        //create student
        Student student = new Student("Aileen", "001", "07123456");
        Student student2 = new Student("Bob", "002", "032434234");

        //create factory to gain information
        StandardRentalFactory factory = new StandardRentalFactory();
        factory.student = student;

        //set rented date
        Calendar calendar = Calendar.getInstance();
        factory.setStartDate(calendar.getTime()); //set sart
        calendar.add(Calendar.DATE, 7); //end day
        factory.setEndDate(calendar.getTime());

        //clean & no rented
        System.out.println("Room " + room1.getRoomNumber() + " is rented: " + room1.isRented());
        System.out.println("Room " + room1.getRoomNumber() + " is available for rent: " + room1.isAvailableForRent());
        System.out.println("========== Test1: No rented & clean condition create Agreement ================");
        try {
            RentalAgreement agreement = factory.createAgreement(room1);
            agreement.executeTerms();
            System.out.println("Rented number: " + factory.leaseNumber);
            System.out.println("Room " + room1.getRoomNumber() + " rented state: " + room1.isRented());
        } catch (InvalidConditionException e) {
            System.out.println("Failed to create agreement: " + e.getMessage());
        }
        System.out.println("========== Test2: In rented condition ================");
        System.out.println("---------rented : in dirty ------------");
        room1.setState(new DirtyState());
        try {
            RentalAgreement agreement = factory.createAgreement(room1);
            agreement.executeTerms();
            System.out.println("Rented number: " + factory.leaseNumber);
            System.out.println("Room " + room1.getRoomNumber() + " rented state: " + room1.isRented());
        } catch (InvalidConditionException e) {
            System.out.println("Failed to create agreement: " + e.getMessage());
        }
        System.out.println("---------rented : in offline ------------");
        room1.setState(new OfflineState());
        try {
            RentalAgreement agreement = factory.createAgreement(room1);
            agreement.executeTerms();
            System.out.println("Rented number: " + factory.leaseNumber);
            System.out.println("Room " + room1.getRoomNumber() + " rented state: " + room1.isRented());
        } catch (InvalidConditionException e) {
            System.out.println("Failed to create agreement: " + e.getMessage());
        }

        System.out.println("========== Test3: In no rented condition ================");
        //In rented condition : change room2 state test if can createAgreement or not
        //测试2:房间状态初始化为clean, 未被租用
        System.out.println("Room " + room2.getRoomNumber() + " is rented: " + room2.isRented());
        System.out.println("Room " + room2.getRoomNumber() + " is available for rent: " + room2.isAvailableForRent());
        //In no rented condition : change room2 state test if can createAgreement or not

        //Bob
        factory.student = student2;
        //clean -> dirt
        room2.setState(new DirtyState());
        try {
            RentalAgreement agreement = factory.createAgreement(room2);
            agreement.executeTerms();
            System.out.println("Rented number: " + factory.leaseNumber);
            System.out.println("Room " + room2.getRoomNumber() + " rented state: " + room2.isRented());
        } catch (InvalidConditionException e) {
            System.out.println("Failed to create agreement: " + e.getMessage());
        }
        System.out.println("---------------------");
        //dirt -> offline
        room2.setState(new OfflineState());
        System.out.println("Room " + room2.getRoomNumber() + " is rented: " + room2.isRented());
        System.out.println("Room " + room2.getRoomNumber() + " is available for rent: " + room2.isAvailableForRent());
        try {
            factory.createAgreement(room2);
        } catch (InvalidConditionException e) {
            System.out.println("Failed to create agreement: " + e.getMessage());
        }
        System.out.println("---------------------");

        //offline -> dirt
        room2.setState(new DirtyState());
        System.out.println("Room " + room2.getRoomNumber() + " is rented: " + room2.isRented());
        System.out.println("Room " + room2.getRoomNumber() + " is available for rent: " + room2.isAvailableForRent());
        try {
            factory.createAgreement(room2);
        } catch (InvalidConditionException e) {
            System.out.println("Failed to create agreement: " + e.getMessage());
        }
        System.out.println("---------------------");

        //dirt -> clean
        room2.setState(new CleanState());
        try {
            RentalAgreement agreement = factory.createAgreement(room2);
            agreement.executeTerms();
            System.out.println("Rented number: " + factory.leaseNumber);
            System.out.println("Room " + room2.getRoomNumber() + " rented state: " + room2.isRented());
        } catch (InvalidConditionException e) {
            System.out.println("Failed to create agreement: " + e.getMessage());
        }

        //Test4: clean -> clean
        System.out.println("========== Test4: Clean to Clean ================");
        room2.setState(new CleanState());
        try {
            RentalAgreement agreement = factory.createAgreement(room2);
            agreement.executeTerms();
            System.out.println("Rented number: " + factory.leaseNumber);
            System.out.println("Room " + room2.getRoomNumber() + " rented state: " + room2.isRented());
        } catch (InvalidConditionException e) {
            System.out.println("Failed to create agreement: " + e.getMessage());
        }

    }
}